/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Main;


import java.io.File;
import plugin.SystemClassLoader;

import system.ExitTimer;
import system.FileTaskMove;
import system.FileTaskRename;
import system.Message;
import system.ReadSettings;
import system.getTimeDate;
import tasks.TaskFinder;
import tasks.getNextTask;

/**
 *
 * @author ZeM
 */
public class StartIDDS {
StartIDDS(){
new Parameters();
            Parameters.TotalTasks = -1;
            Parameters.TasksDone = 0;
            Parameters.exit = false;
            Parameters.ExitTimerIsWorking = false;


            //reading settings file
            new ReadSettings();
            System.out.println(new Message("s0000").message);
            //setting some parameters
            Parameters.TaskName = new String[Parameters.MaxTaskNumber];
            Parameters.taskAssignTime = new String[Parameters.MaxTaskNumber];
            if (Parameters.DeleteTaskWhenDone == 1 | (Parameters.DeleteTaskWhenDone == 0 && Parameters.MoveDoneTask == 1)) {
                Parameters.AdrOfDoneTasks = new String[Parameters.MaxTaskNumber];
            }
            //runing user interface
            new SystemClassLoader("iddsCUI.jar");
            
            //main part:

            while (!Parameters.exit) { // if there is no need to exit
                //getting current system time
                Parameters.CurrentSystemTime = new getTimeDate("yyyyMMddHHmmss").datetime;
                try {
                    if (Parameters.TotalTasks == 0) {

                        Thread.sleep(Parameters.FindTaskPause * 1000);
                    }
                } catch (InterruptedException ex) {
                    System.err.println(ex);
                }
                //scan folder for task files
                new TaskFinder().Find();

                //checking if we have tasks to do
                if (Parameters.TotalTasks > 0) {
                    //Printing information about how much tasks left to do
                    Parameters.message_parameter[0] = "" + Parameters.TotalTasks;
                    System.out.println(new Message("ev0001").message);
                    //getting current system time
                    Parameters.CurrentSystemTime = new getTimeDate("yyyyMMddHHmmss").datetime;
                    //getting task adress
                    new getNextTask();

                    if (Parameters.CurrentTaskNumber > -1) {
                        if (Parameters.ExitTimerIsWorking) {
                            ExitTimer.stop = true;
                        }

                        String adress_of_new_task = Parameters.TaskName[Parameters.CurrentTaskNumber];

                        //start doing task
                        Parameters.TaskFailed = false;


                        /////////////////////////////////////////
                        //              TASK                   //
                        /////////////////////////////////////////
                        if (Parameters.DeleteTaskWhenDone == 1) {//delete task
                            new File("" + Parameters.TaskFolder + Parameters.TaskName[Parameters.CurrentTaskNumber]).delete();
                        } else if (Parameters.MoveDoneTask == 1 | Parameters.MoveFailedTask == 1) {//move task when done or fail
                            new FileTaskMove();

                        } else if (Parameters.MoveDoneTask == 2 | Parameters.MoveFailedTask == 2) {//rename task when done or fail
                            new FileTaskRename();

                        }


                        if (Parameters.TotalTasks > 1) {//if it's not the last task
                            Parameters.TaskName[Parameters.CurrentTaskNumber] = Parameters.TaskName[Parameters.TotalTasks - 1];
                            Parameters.taskAssignTime[Parameters.CurrentTaskNumber] = Parameters.taskAssignTime[Parameters.TotalTasks - 1];
                        }

                        if (Parameters.DeleteTaskWhenDone == 1 | (Parameters.DeleteTaskWhenDone == 0 && (Parameters.MoveDoneTask == 1) | Parameters.MoveFailedTask == 1)) {
                            Parameters.AdrOfDoneTasks[Parameters.TasksDone] = adress_of_new_task;
                            Parameters.TasksDone++;
                        }

                    }



                } else if (Parameters.QuitTime != -1 && !Parameters.exit) { //if no more tasks left ant exit time >=0
                    if (!Parameters.ExitTimerIsWorking) {
                        //start timer
                        Parameters.message_parameter[0] = "" + Parameters.QuitTime;
                        System.out.println(new Message("ev0002").message);
                        Thread timer = new Thread(new ExitTimer());
                        timer.start();

                    }

                }
            }

}
}
